package com.youdianda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youdianda.context.BaseContext;
import com.youdianda.dto.AdvposListDTO;
import com.youdianda.dto.PosAllDTO;
import com.youdianda.dto.YddAdvposDTO;
import com.youdianda.dto.YddSelectDTO;
import com.youdianda.entity.YddAdvimg;
import com.youdianda.entity.YddAdvpos;

import com.youdianda.mapper.YddAdvposMapper;
import com.youdianda.result.PageResult;
import com.youdianda.result.Result;
import com.youdianda.service.IYddAdvimgService;
import com.youdianda.service.IYddAdvposService;


import com.youdianda.vo.YddAdvimgVO;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.xml.crypto.Data;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Currency;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sdy
 * @since 2023-05-15
 */
@Service
@Transactional
public class YddAdvposServiceImpl extends ServiceImpl<YddAdvposMapper, YddAdvpos> implements IYddAdvposService {

    @Autowired
    private YddAdvposMapper yddAdvposMapper;
    @Autowired
    private IYddAdvimgService yddAdvimgService;
    /**
     * 查询
     * @param id
     * @return
     */
    @Override
    public Result<YddAdvpos> Selectpos(Integer id) {


       YddAdvpos yddAdvpos= yddAdvposMapper.selectById(id);
        return Result.success(yddAdvpos);
    }

    /**
     * 修改
     * @param yddAdvpos
     * @return
     */
    @Override
    public Result update(YddAdvpos yddAdvpos) {

        int i = yddAdvposMapper.updateById(yddAdvpos);

        return Result.success(i);
    }



    /**
     * 广告位列表-分页
     *
     * @param advposListDTO
     * @return
     */
    @Override
    public Result<PageResult> listAdvpos(AdvposListDTO advposListDTO) {

        Page<YddAdvpos> page = new Page<>(advposListDTO.getPage(),advposListDTO.getLimit());

        Page<YddAdvpos> advposPage = page(page, Wrappers.<YddAdvpos>lambdaQuery().like(advposListDTO.getAdvposname() != null, YddAdvpos::getAdvposname, advposListDTO.getAdvposname())
                .like(advposListDTO.getAdvposdesc() != null, YddAdvpos::getAdvposdesc, advposListDTO.getAdvposdesc()));


        ArrayList<YddAdvimgVO> yddAdvimgVOS = new ArrayList<>();
        advposPage.getRecords().forEach(yddAdvpos -> {
            YddAdvimgVO yddAdvimgVO = new YddAdvimgVO();
            BeanUtils.copyProperties(yddAdvpos,yddAdvimgVO);
            long count = yddAdvimgService.count(Wrappers.<YddAdvimg>lambdaQuery().eq(YddAdvimg::getAdvimgpos, yddAdvpos.getId()));
            yddAdvimgVO.setAdvposnum(count+"");
            yddAdvimgVOS.add(yddAdvimgVO);
        });



        return Result.success( PageResult.builder()
                .count((int) advposPage.getTotal())
                .totalPages((int) advposPage.getPages())
                .pageSize((int) advposPage.getSize())
                .currentPage((int) advposPage.getCurrent())
                .data(yddAdvimgVOS).build());
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public Result delete(Integer id) {

        yddAdvposMapper.deleteById(id);

        return Result.success(id);
    }

    /**
     * 新增
     * @param dto
     * @return
     */
    @Override
    public Result add(YddSelectDTO dto) {


        YddAdvpos entiy = new YddAdvpos();
        BeanUtils.copyProperties(dto, entiy);


        save(entiy);


        return Result.success();


    }



    @Override
    public Result getYddAdvPosAll( ) {


        List<YddAdvpos> list = list();

        return Result.success(list);
    }


}
